/*http请求*/
const rp = require('request-promise');
const tough = require('tough-cookie');
/*音乐工具*/
const crypto = require('../../../utils/crypto.js');

/**
 * @apiDefine UserNotFoundError
 *
 * @apiError UserNotFound The id of the User was not found.
 *
 * @apiErrorExample Error-Response:
 *     HTTP/1.1 404 Not Found
 *     {
 *       "error": "UserNotFound"
 *     }
 */


/**
 * @api {post} /music/wy/search 网易云搜索列表
 * @apiName /wy/search
 * @apiGroup music
 *
 * @apiParam {String} val 搜索关键字.
 *
 * @apiSuccess {String} code 200 数据成功.
 * @apiSuccess {Object} result  数据结果对象.
 * 
 * @apiSuccessExample Success-Response:
 * code: 200
 * result: {songs: [,…], songCount: 178}
 * 
 * @apiUse UserNotFoundError
 * 
 */
exports.search = async (ctx, next) => {

  /*参数声明*/
  let body = ctx.request.body;
  let str = body.val;
  
  let params = crypto.weapi({
		's': str,
		'offset': 0,
		'limit': 30,
		'type': 1
  });
  
  let cookie = new tough.Cookie({
      key: 'appver',
      value: '2.0.2',
      domain: 'music.163.com',
      httpOnly: true,
      maxAge: 31536000
  });

  var cookiejar = rp.jar();
  cookiejar.setCookie(cookie, 'http://music.163.com');

  /*函数体*/
  let options = {
    method: 'POST',
    uri: 'http://music.163.com/weapi/cloudsearch/get/web?csrf_token=',
    form: params,
    headers: {
      'Referer': 'http://music.163.com',
      'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
    },
    json: true // Automatically parses the JSON string in the response
  };
 
  let res;
  await rp(options)
    .then(function (repos) {
      res = repos;
    })
    .catch(function (err) {
      res = err;
    });

  return ctx.body = res;
}


/**
 * @api {post} /music/wy/getUrl 网易云获取歌曲url
 * @apiName /wy/getUrl
 * @apiGroup music
 *
 * @apiParam {String} songId 歌曲Id.
 *
 * @apiSuccess {String} code 200 数据成功.
 * @apiSuccess {Object} data 列表对象集合.
 * 
 * @apiSuccessExample Success-Response:
 * code: 200
 * data: [{id: 1370008169,…}]
 * 
 * @apiUse UserNotFoundError
 */
exports.getUrl = async (ctx, next) => {

  /*参数声明*/
  let body = ctx.request.body;
  
  let params =  crypto.weapi({
    'csrf_token': '',
    'ids': '['+body.songId+']',
    'br': body.songId || '320000'
  });
  
  let cookie = new tough.Cookie({
    key: 'appver',
    value: '2.0.2',
    domain: 'music.163.com',
    httpOnly: true,
    maxAge: 31536000
});

var cookiejar = rp.jar();
cookiejar.setCookie(cookie, 'http://music.163.com');

/*函数体*/
let options = {
  method: 'POST',
  uri: 'https://music.163.com/weapi/song/enhance/player/url',
  form: params,
  headers: {
    'Referer': 'http://music.163.com',
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
  },
  json: true // Automatically parses the JSON string in the response
};
console.log('options',options)
let res;
await rp(options)
  .then(function (repos) {
    res = repos;
  })
  .catch(function (err) {
    res = err;
  });

return ctx.body = res;
}


// qq

exports.qqsearch = async (ctx, next) => {

  let limit = 30;
  let page = 0;
  

  /*参数声明*/
  let body = ctx.request.body;
  let str = body.val;

  /*函数体*/
  let options = {
    uri: `https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=sizer.yqq.song_next&searchid=49252838123499591&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=${page}&n=${limit}&w=${encodeURIComponent(str)}&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0`,
    headers: {
      'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
    },
    json: true // Automatically parses the JSON string in the response
  };

  let res;
  await rp(options)
    .then(function (repos) {
      res = repos;
    })
    .catch(function (err) {
      res = err;
    });

  return ctx.body = res;
}


exports.qqgetUrl = async (ctx, next) => {

  let songId = 7544244;
  let type = 0;

  let bHh = '624868746c'

  /*参数声明*/
  let body = ctx.request.body;


  // https://v1.itooi.cn/tencent/url?id=${id}&quality=[128,320,flac]

  /*函数体*/
  let options = {
    uri: `https://v1.itooi.cn/tencent/url?id=${songId}&quality=[128,320,flac]&isRedirect=0`,
    headers: {
      'User-Agent': 'lx-music request',
      [bHh]: [bHh],
      "timeout": '15000',
      'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
    },
    json: true // Automatically parses the JSON string in the response
  };

  let res;
  await rp(options)
    .then(function (repos) {
      res = repos;
    })
    .catch(function (err) {
      res = err;
    });

  return ctx.body = res;
}



// 酷我
exports.kwsearch = async (ctx, next) => {

  let limit = 30;
  let page = 0;

  /*参数声明*/
  let body = ctx.request.body;
  let str = body.val;

  

  /*函数体*/
  let options = {
     uri: `http://www.kuwo.cn/api/www/search/searchMusicBykeyWord?key=${encodeURIComponent(str)}&pn=${page}&rn=${limit}&reqId=b20e7880-8124-11ea-b847-451e2a0db1ff`,
    headers: {
      'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
    },
    json: true // Automatically parses the JSON string in the response
  };

  let res;
  await rp(options)
    .then(function (repos) {
      res = repos;
    })
    .catch(function (err) {
      res = err;
    });

  return ctx.body = res;
}

// 酷狗
exports.kgsearch = async (ctx, next) => {

  let limit = 20;
  let page = 1;

  /*参数声明*/
  let body = ctx.request.body;
  let str = body.val;

  /*函数体*/
  let options = {
    uri: `http://ioscdn.kugou.com/api/v3/search/song?keyword=${encodeURIComponent(str)}&page=${page}&pagesize=${limit}&showtype=10&plat=2&version=7910&tag=1&correct=1&privilege=1&sver=5`,
    headers: {
      'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36'
    },
    json: true // Automatically parses the JSON string in the response
  };

  let res;
  await rp(options)
    .then(function (repos) {
      res = repos;
    })
    .catch(function (err) {
      res = err;
    });

  return ctx.body = res;
}


